package com.ujiuye.service.impl;


import com.ujiuye.dao.UserDao;
import com.ujiuye.dao.impl.UserDaoImpl;
import com.ujiuye.entity.User;
import com.ujiuye.service.UserService;
import com.ujiuye.util.PageBean;

import java.util.List;

public class UserServiceImpl implements UserService {
     UserDao ud= new UserDaoImpl();
    @Override
    public User checkUser(String username, String password) {
        return ud.checkUser(username,password);
    }

    @Override
    public PageBean<User> findAllByPage(String currentPage1, String pageSize1, String username) {
        //判断currentPage是否为空
        if (currentPage1 == null || currentPage1.trim().equals("")) {
            currentPage1 = "1";
        }
        if (pageSize1 == null || pageSize1.trim().equals("")) {
            pageSize1 = "3";
        }

        //封装page对象
        int currentPage = Integer.parseInt(currentPage1);
        int pageSize = Integer.parseInt(pageSize1);
        //封装总条数
        int totalCount = ud.totalCount(username);
        //封装总页数
        int totalPage = totalCount%pageSize==0 ? totalCount/pageSize :totalCount/pageSize+1;
        //封装数据
        int startIndex = pageSize * (currentPage-1);//起始的航行下标
        List<User> list = ud.selectAllUser(startIndex,pageSize,username);

        //创建一个PageBean对象
        PageBean<User> pb = new PageBean<>(currentPage,pageSize,totalCount,totalPage,list);

        return pb;

    }

    @Override
    public int addUser(User user) {
        return ud.addUser(user);
    }

    @Override
    public int delAll(String uids) {
        return ud.delAll(uids);
    }

    @Override
    public int updateUser(User user) {
        return ud.updateUser(user);
    }

    @Override
    public User checkPhone(String tel) {
        return ud.checkPhone(tel);
    }

    @Override
    public int registerUser(String name, String password, String phone) {
        return ud.registerUser(name,password,phone);
    }

    @Override
    public User beforeLogin(String password, String phone) {
        return ud.beforeLogin(password,phone);
    }


}
